package cn.jyc.ssm02.ctrl;

import cn.jyc.ssm02.pojo.User;
import cn.jyc.ssm02.service.UserServ;
import cn.jyc.ssm02.utls.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Controller
public class UserCtrl {
    @Autowired
    private UserServ us;

    @ResponseBody
    @RequestMapping("/qry")
    public String qry(@RequestParam(value = "page",defaultValue = "1") Integer p1,
                      @RequestParam(value = "rows",defaultValue = "10") Integer p2) {
        Map<String, Object> qry = us.qry(p1, p2);
        return JsonUtil.objectToJson(qry);
    }

    //    name: 张八
//    sex: 男
//    birthd: 2022-04-11
//    addr: 西安市高新区
//    email: zb@qq.com
    @ResponseBody
    @RequestMapping("/add")
    public String add(String name,String sex,String birthd,String addr,String email) {
        System.out.println(LocalTime.now()+"---"+name+"/"+sex+"/"+birthd+"/"+addr+"/"+email);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        User user = null;
        try {
            user = new User(name, sex, sdf.parse(birthd), addr, email);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(LocalTime.now() + "---" + user);
        Map<String, Object> res = new HashMap<>();
        try {
//            us.add(new User(name,sex,sdf.parse(birthd),addr,email));
            us.add(user);
//            throw new RuntimeException("异常抛出测试");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            res.put("errorMsg", e.getMessage());
        }
        return JsonUtil.objectToJson(res);
    }
}
